﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HsTec.Mobile.Green.SqlServerDAL;
using Baimasoft.FrameWork.Base;
using Baimasoft.Web.UI;


public partial class BaseData_AddAreaForm : Baimasoft.Web.UI.PageFormTop
{
    protected int addtype = 0;
    protected string arename = string.Empty;
    protected int EditStatus1 = 0;
    Area MyArea = new Area();


    protected override void InitForm()
    {
        addtype = (Request.QueryString["parentID"]) == null ? 0 : int.Parse(Request.QueryString["parentID"].ToString());
        arename = Request.QueryString["areaname"] == null ? "" : Request.QueryString["areaname"].ToString();
        EditStatus1 = int.Parse(Request.QueryString["EditStatus"].ToString());
        if (this.EditStatus == EditStatus.Add)
        {
            EditStatus1 = int.Parse(Request.QueryString["EditStatus"].ToString());
            this.txtChildAreaID.Text = Convert.ToString(MyArea.GetMaxChildAreaID(addtype) + 1);
        }
        else if (this.Request.Params["ID"] != null)
        {
            EditStatus1 = int.Parse(Request.QueryString["EditStatus"].ToString());

            DataTable MyDT = MyArea.GetAreaInfos(" ID= " + this.Request.Params["ID"].ToString().Trim());
            foreach (DataRow dr in MyDT.Rows)
            {
                this.txtName.Text = dr["Name"].ToString().Trim();//大类名称
                this.txtChildArea.Text = dr["AreaID"].ToString().Trim();//排序
            }
        }
    }
    /// <summary>
    /// 保存数据
    /// </summary>
    protected override void SavaData()
    {
        try
        {
            string sid = "";

            if (this.Request.Params["ID"] != null)
            {
                sid = this.Request.Params["ID"].Trim();
            }
            else
            {
                sid = "-1";
            }

            int areaid = 0;
            string sName = "";
            int parentId = Request.QueryString["parentID"] == null ? -1 : int.Parse(Request.QueryString["parentID"].ToString());

            string sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();
            if (sEditStatus == "1")
            {
                areaid = System.Convert.ToInt32(this.txtChildAreaID.Text.ToString());
                sName = this.txtChildName.Text;//区域名称
                int iEffective = 1;
                int iParentID = parentId;

                int iback = MyArea.InsertInfo(areaid, iParentID, sName, "0", iEffective, 0, 0, null, null, null, null, null, null);
                if (iback > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功!');window.close();</script>");
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败!');</script>");
                    return;
                }
            }
            else
            {
                areaid = System.Convert.ToInt32(this.txtChildArea.Text.ToString());
                sName = this.txtName.Text;//区域名称
                int iEffective = 1;

                int iback = 0;
                try
                {
                    iback = MyArea.UpdateInfo(areaid, parentId, sName, "0", iEffective, 0, 0, sid);
                }
                catch
                {
                }

                if (iback > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');window.close();</script>");
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败!');</script>");
                    return;
                }
            }
        }
        catch (Exception ex)
        {
            Message.AlertMsg(this, "保存失败");
        }
    }

}
